layui.use(['laydate','form','table','laypage'], function(){
  var table = layui.table;
  var form = layui.form,
	layer = layui.layer;
  var laydate = layui.laydate;

    testObj = {
    init: function(){
        this.bindEvent();
    },
    bindEvent: function(){
        // 开始时间
        var start = laydate.render({
            elem: '#start',
            trigger: 'click',
            btns: ['clear', 'confirm'],
            // showBottom: false,
            done: function (value, date) {
                end.config.min = {
                    year: date.year,
                    month: date.month - 1,
                    date: date.date,
                    hours: date.hours,
                    minutes: date.minutes,
                    seconds: date.seconds
                };
                // 作为 结束选择 的 开始时间
                end.config.value = value;
            }
        });
        // 结束时间
        var end = laydate.render({
            // 绑定元素
            elem: '#end',
            //  触发方式
            trigger: 'click',
            // 底部按钮
            btns: ['clear', 'confirm'],
            // showBottom: false,
            // 选择完成回调
            done: function (value, date) {
                start.config.max = {
                    year: date.year,
                    month: date.month - 1,
                    date: date.date,
                    hours: date.hours,
                    minutes: date.minutes,
                    seconds: date.seconds
                };
                start.config.value = value;
            }
        });
        laydate.render(start);
        laydate.render(end);
    }
}
testObj.init()
    
	/*初始化表格*/
	function tableInit(where = {})
	{  
	  table.render({
		elem: '#dataTables-example'
		,url:'/admin/goods/stockgoodslist.html'
		,where:where
		,method:'POST'
		,parseData: function(res){ //res 即为原始返回的数据
			//封装返回数据
			console.log(res);
            for (i in res.data) {
				urls = '/admin/goods/stockgoodsinfo/id/'+res.data[i]['stock_id']+'.html';
				res.data[i]['reason'] = "<button class='layui-btn' onclick=\"xadmin.open('详情','"+urls+"',800,500)\">详情</button>";
			}
            
			return {
			  "code": 10001, //解析接口状态
			  "msg": '', //解析提示文本
			  "count": res.total, //解析数据长度
			  "data": res.data //解析数据列表
			};
		}
		,cols: [[
		  {field:'stock_id', title: '序号'}
          ,{field:'shop_id', title: '所属门店'} 
          ,{field:'addtime', title: '入库时间'} 
		  ,{field:'goods_name', title: '产品名称'}
		  ,{field:'pur_number', title: '采购数量'}
          ,{field:'number', title: '实际数量'}
		  ,{field:'unit', title: '单位'}
		  ,{field:'buying_price', title: '进货单价（元）'}
          ,{field:'total_money', title: '合计进货总价（元）'}
		  ,{field:'supplier_id', title: '供应商'}
		  ,{field:'contract_sn', title: '合同编号'}
		  ,{field:'reason', title: '操作'}
		]]
		,page: { //支持传入 laypage 组件的所有参数（某些参数除外，如：jump/elem） - 详见文档
		  layout: ['prev', 'page', 'next', 'skip', 'count']
		}
		,response: {
			statusCode: 10001
		}
	  });
	}
	tableInit();
	
	//监听提交
	form.on('submit(sreach)',
	function(data) {
		console.log(data.field);
		tableInit(data.field);
		return false;
	});
});

